home *** CD-ROM | disk | FTP | other *** search
- @z --- f90_cpp.web ---
-
- This file is included in fwebman.tex.
-
- Author: J. A. Krommes
- Version: 1.23
- Date: April 1, 1992
-
- @x-----------------------------------------------------------------------------
-
- @r9[-r/ -W[]
- @* INTRODUCTION. This example demonstrates operator overloading in both
- Ratfor--90 and~C$++$. It also shows how brackets may be used instead of
- parentheses for \Fortran\ array indices.
- @l "\\let\\WARRAY\\WSUB" // Subscript \Fortran\ indices.
- @a
- @<Ratfor--90@>@;
- @c++
- @<C$++$@>@;
-
- @ The following example is excerpted from the ANSI Draft~S8, Version~112
- for the Fortran--90 language.
-
- @v .IN. "\\in" < /* Make \.{.IN.} display as~`$\in$' and be treated as a
- relational operator. */
- @v <= "\\subset" <= /* Make \.{.LE.} display as~`$\subset$' and be treated
- as a relational operator. */
-
- @<R...@>=
-
- module integer_sets
- {
- integer, parameter :: max_set_card = 200;
-
- type set
- {
- private:
- integer card;
- integer element[max_set_card];
- };
-
- interface operator(.IN.)
- {
- module procedure element;
- };
-
- interface operator(<=)
- {
- module procedure subset;
- };
-
- @<Union function@>@;
- @<Subset function@>@;
- }
-
- @ This function uses structure elements. We overload the element
- operator~`\.{\%}' to make the resulting code look more like~\C.
-
- @v % "." . // Make \.\% print as~`\..' and also be treated as~`\..'.
-
- @<Union function@>=
-
- function union(A,B)
- type(set) A,B;
- {
- type(set) UNION;
- integer j;
-
- UNION = A;
- do j=1,B%card;
- if(!(B%element[j] .IN. A))
- if(UNION%card < max_set_card)
- {
- UNION%card += 1;
- UNION%element[UNION%card] =
- B%element[j];
- }
- else ; // Maximum set size exceeded...
- }
-
- @ We claim that this is much more visually appealing than a raw listing.
- @<Subset function@>=
-
- logical function subset(A,B)
- type(set) A,B;
- {
- integer i;
-
- subset = A%card <= B%card; /* In the source, this is ``\.{subset\ =\
- A\%card\ <=\ B\%card;}'' */
- if(!subset) return;
-
- do i=1,A%card;
- subset = subset && (A%element[i] .IN. B);
- }
-
- @ Here is a short example of operator overloading in~\Cpp. Note that since the
- global language is \Ratfor--90, we must explicitly insert a language
- command in the following definition section in order that the star be
- overloaded in the proper language.
- @c++
- @v * "\\times" *
- @<C...@>=
-
- class complex
- {
- double re,im;
-
- public:
- complex(double r,double i) {re=r; @+ im=i;}
- friend complex operator+(complex, complex);
- friend complex operator*(complex, complex);
- };
-
- z = x*y; /* An example of a statement using the overloading multiplication
- operator. */
-
- @* INDEX.
-